package com.mr.groupcomparator;

import java.io.IOException;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;

public class GroupComparatorReducer extends Reducer<GroupComparator, NullWritable, GroupComparator, NullWritable>{

	/*
	 * key-value(nullWritable)在reducer工作期间只会实例化一个key-value对象
	 * 每次调用迭代器迭代下一个记录时，使用反序列化器从文件中或内存中读取下一个key-value数据的值，
	 * 封装到之前key-value的reducer属性中
	 */
	@Override
	protected void reduce(GroupComparator key, Iterable<NullWritable> values,
						  Reducer<GroupComparator, NullWritable, GroupComparator, NullWritable>.Context context)
			throws IOException, InterruptedException {

		Double maxAcount = key.getAcount();

		for (NullWritable nullWritable : values) {

			// 如果当前key的acount与最大的acount相等就输出
			if(key.getAcount().equals(maxAcount)) {
				context.write(key, nullWritable.get());
			}
		}

	}
}
